﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Transactions;
using System.Data.SqlClient;

using Global.Class.Library;
using DataTransferObject;
using DataTransferObject.MetaDataList;
using DataAccessLayer;
using DataAccessLayer.MetaDataList;
using DataAccessLayer.MetaDataList.ListDistributorNameDTSTableAdapters;

namespace BusinessLogicLayer.MetaDataList
{
    public class ListDistributorNameBLL : NotifyPropertyChangeObject
    {
        public GlobalEnum.TaskID TaskID { get { return GlobalEnum.TaskID.ListDistributorName; } }

        private UserInformation userOrganization;

        private ListDistributorNameMaster listDistributorNameMaster;

        private BindingList<ListDistributorNameDetail> listDistributorNameDetailList;



        public ListDistributorNameBLL()
        {
            try
            {
                if (GlobalVariables.shouldRestoreProcedure) RestoreProcedure();


                userOrganization = new UserInformation();

                listDistributorNameMaster = new ListDistributorNameMaster();

                this.listDistributorNameDetailList = new BindingList<ListDistributorNameDetail>();

                GlobalDefaultValue.Apply(this);


                this.ListDistributorNameMaster.PropertyChanged += new PropertyChangedEventHandler(ListDistributorNameMaster_PropertyChanged);

                this.ListDistributorNameDetailList.ListChanged += new ListChangedEventHandler(ListDistributorNameDetailList_ListChanged);

            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        private void ListDistributorNameMaster_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.SetDirty();
        }

        private void ListDistributorNameDetailList_ListChanged(object sender, ListChangedEventArgs e)
        {
            this.SetDirty();
        }




        #region <Adapter>

        private ListDistributorNameListingTableAdapter listingTableAdapter;
        protected ListDistributorNameListingTableAdapter ListingTableAdapter
        {
            get
            {
                if (listingTableAdapter == null) listingTableAdapter = new ListDistributorNameListingTableAdapter();
                return listingTableAdapter;
            }
        }

        private ListDistributorNameTableAdapter masterTableAdapter;
        protected ListDistributorNameTableAdapter MasterTableAdapter
        {
            get
            {
                if (masterTableAdapter == null) masterTableAdapter = new ListDistributorNameTableAdapter();
                return masterTableAdapter;
            }
        }

        private ListDistributorNameDetailTableAdapter detailTableAdapter;
        protected ListDistributorNameDetailTableAdapter DetailTableAdapter
        {
            get
            {
                if (detailTableAdapter == null) detailTableAdapter = new ListDistributorNameDetailTableAdapter();
                return detailTableAdapter;
            }
        }


        #endregion <Adapter>

        #region <Storage>

        public UserInformation UserOrganization
        {
            get { return this.userOrganization; }
            set { this.userOrganization = value; }
        }

        public ListDistributorNameDTS.ListDistributorNameListingDataTable ListDistributorNameListing()
        {
            return this.ListingTableAdapter.GetData();
        }

        public ListDistributorNameMaster ListDistributorNameMaster
        {
            get { return this.listDistributorNameMaster; }
        }

        public BindingList<ListDistributorNameDetail> ListDistributorNameDetailList
        {
            get { return this.listDistributorNameDetailList; }
        }


        #endregion <Storage>

        #region Properties

        #region <Primary Key>

        public int DistributorID   //Primary Key
        {
            get { return this.ListDistributorNameMaster.DistributorID; }
            private set
            {
                if (this.ListDistributorNameMaster.DistributorID != value)
                {
                    this.StopTracking();

                    this.ListDistributorNameMaster.DistributorID = value;

                    this.ListDistributorNameGetMaster();
                    this.ListDistributorNameGetDetail();

                    this.StartTracking();
                    this.Reset();
                }

            }
        }

        #endregion <Primary Key>

        private void ListDistributorNameGetMaster()
        {
            if (this.DistributorID > 0)
            {
                ListDistributorNameDTS.ListDistributorNameDataTable masterDataTable = this.MasterTableAdapter.GetData(this.DistributorID);

                if (masterDataTable.Count > 0)
                {
                    this.ListDistributorNameMaster.StopTracking();

                    this.ListDistributorNameMaster.EntryDate = masterDataTable[0].EntryDate;

                    this.ListDistributorNameMaster.DistributorCategoryID = masterDataTable[0].DistributorCategoryID;
                    this.ListDistributorNameMaster.DistributorTypeID = masterDataTable[0].DistributorTypeID;
                    this.ListDistributorNameMaster.AddressAreaID = masterDataTable[0].AddressAreaID;

                    this.ListDistributorNameMaster.Description = masterDataTable[0].Description;
                    this.ListDistributorNameMaster.DescriptionOfficial = masterDataTable[0].DescriptionOfficial;
                    this.ListDistributorNameMaster.DescriptionOriginal = masterDataTable[0].DescriptionOriginal;
                    this.ListDistributorNameMaster.VATCode = masterDataTable[0].VATCode;
                    this.ListDistributorNameMaster.ContactName = masterDataTable[0].ContactName;
                    this.ListDistributorNameMaster.AttentionName = masterDataTable[0].AttentionName;
                    this.ListDistributorNameMaster.Telephone = masterDataTable[0].Telephone;
                    this.ListDistributorNameMaster.EMailAddress = masterDataTable[0].EMailAddress;
                    this.ListDistributorNameMaster.AddressNo = masterDataTable[0].AddressNo;
                    this.ListDistributorNameMaster.AddressStreet = masterDataTable[0].AddressStreet;
                    this.ListDistributorNameMaster.Other = masterDataTable[0].Other;

                    this.ListDistributorNameMaster.InActive = masterDataTable[0].InActive;

                    this.ListDistributorNameMaster.StartTracking();

                    this.ListDistributorNameMaster.Reset();

                    this.UserOrganization.UserID = masterDataTable[0].UserID;
                    this.UserOrganization.UserOrganizationID = masterDataTable[0].UserOrganizationID;
                }
                else throw new System.ArgumentException("Insufficient get data");
            }
            else
            {
                GlobalDefaultValue.Apply(this.ListDistributorNameMaster);
                this.ListDistributorNameMaster.EntryDate = DateTime.Today;
                this.ListDistributorNameMaster.Reset();
            }
        }


        private void ListDistributorNameGetDetail()
        {
            this.listDistributorNameDetailList.RaiseListChangedEvents = false;
            this.listDistributorNameDetailList.Clear();
            if (this.DistributorID >= 0)
            {

                ListDistributorNameDTS.ListDistributorNameDetailDataTable detailDataTable = this.DetailTableAdapter.GetData(this.DistributorID);

                if (detailDataTable.Count > 0)
                {
                    foreach (ListDistributorNameDTS.ListDistributorNameDetailRow detailRow in detailDataTable.Rows)
                    {
                        this.listDistributorNameDetailList.Add(new ListDistributorNameDetail(detailRow.DistributorModelID, detailRow.Reference, detailRow.Description, detailRow.Other));
                    }
                }
            }
            this.listDistributorNameDetailList.RaiseListChangedEvents = true;
            this.listDistributorNameDetailList.ResetBindings();
        }

        #endregion Properties





        #region Object Permission

        public override bool IsValid
        {
            get
            {
                List<ListDistributorNameDetail> inValidListDistributorNameDetail = this.ListDistributorNameDetailList.Where(listDistributorNameDetail => !listDistributorNameDetail.IsValid).ToList();
                return this.ListDistributorNameMaster.IsValid && inValidListDistributorNameDetail.Count == 0;
            }
        }

        public bool ReadOnly
        {
            get
            {
                try
                {
                    return GlobalUserPermission.GetUserReadOnly(GlobalVariables.GlobalUserInformation.UserID, this.TaskID);
                }
                catch
                {
                    return true;
                }
            }
        }

        public bool Editable
        {
            get
            {
                try
                {
                    if (!GlobalUserPermission.GetUserEditable(GlobalVariables.GlobalUserInformation.UserID, this.TaskID, this.UserOrganization.UserOrganizationID)) return false;

                    return GlobalUserPermission.GetEditable("ListDistributorNameEditable", this.DistributorID);
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Verifiable
        {
            get
            {
                try
                {
                    if (!GlobalUserPermission.GetUserEditable(GlobalVariables.GlobalUserInformation.UserID, GlobalEnum.TaskID.ListDistributorNameVerifiable, this.UserOrganization.UserOrganizationID)) return false;

                    return GlobalUserPermission.GetEditable("ListDistributorNameEditable", this.DistributorID);
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Unverifiable
        {
            get
            {
                try
                {
                    if (!GlobalUserPermission.GetUserEditable(GlobalVariables.GlobalUserInformation.UserID, GlobalEnum.TaskID.ListDistributorNameUnverifiable, this.UserOrganization.UserOrganizationID)) return false;

                    return GlobalUserPermission.GetEditable("ListDistributorNameEditable", this.DistributorID);
                }
                catch
                {
                    return false;
                }
            }
        }


        #endregion Object Permission






        public void Fill(int distributorID)
        {
            if (this.DistributorID == distributorID) this.DistributorID = -1; //Enforce to reload
            this.DistributorID = distributorID;
        }

        public void New()
        {
            if (this.DistributorID == 0) this.DistributorID = -1;
            this.DistributorID = 0;
        }

        public void Edit()
        {

        }

        #region Save & Delete Method


        public bool Save()
        {
            int distributorID = 0;

            try
            {

                if (!this.SaveValidate()) throw new System.ArgumentException("Insufficient save", "Save validate");

                using (TransactionScope transactionScope = new TransactionScope())
                {
                    if (this.ReadOnly) throw new System.ArgumentException("Insufficient save", "Uneditable");

                    if (!this.SaveMaster(ref distributorID)) throw new System.ArgumentException("Insufficient save", "Save master");

                    if (!this.SaveDetail(distributorID)) throw new System.ArgumentException("Insufficient save", "Save detail");

                    transactionScope.Complete();
                }

                //SQLDatabase.ExecuteNonQuery("ListDistributorNameUpdateHierarchyLevel", CommandType.StoredProcedure, "AncestorID", this.listDistributorNameMaster.AncestorID);

                this.Fill(distributorID);
                return true;
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
        }


        public bool Delete()
        {
            if (this.listDistributorNameMaster.DistributorID <= 0) return false;

            try
            {

                if (!this.SaveValidate()) throw new System.ArgumentException("Insufficient delete", "Delete validate");

                using (TransactionScope transactionScope = new TransactionScope())
                {
                    if (!this.Editable) throw new System.ArgumentException("Insufficient delete", "Uneditable");

                    if (!this.SaveUndo(this.listDistributorNameMaster.DistributorID)) throw new System.ArgumentException("Insufficient delete", "Delete detail");

                    if (this.MasterTableAdapter.Delete(this.listDistributorNameMaster.DistributorID) != 1) throw new System.ArgumentException("Insufficient delete", "Delete master");

                    transactionScope.Complete();
                }
                this.Fill(0);
                return true;
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
        }


        private bool SaveValidate()
        {
            ExceptionTable exceptionTable = new ExceptionTable(new string[2, 2] { { "ExceptionCategory", "System.String" }, { "ExceptionDescription", "System.String" } });

            this.UserOrganization = GlobalUserPermission.GetUserInformation(this.UserOrganization.UserID, this.ListDistributorNameMaster.EntryDate);

            if (this.UserOrganization.UserID <= 0 || this.UserOrganization.UserOrganizationID <= 0) exceptionTable.AddException(new string[] { GlobalVariables.stringFieldRequired, "User information" });

            if (exceptionTable.Table.Rows.Count <= 0 && this.IsValid) return true; else throw new CustomException("Save validate", exceptionTable.Table);
        }


        private bool SaveMaster(ref int distributorID)
        {
            ListDistributorNameDTS.ListDistributorNameDataTable masterDataTable;
            ListDistributorNameDTS.ListDistributorNameRow masterRow;

            if (this.listDistributorNameMaster.DistributorID <= 0) //Add
            {
                masterDataTable = new ListDistributorNameDTS.ListDistributorNameDataTable();
                masterRow = masterDataTable.NewListDistributorNameRow();
            }
            else //Edit
            {
                if (!this.SaveUndo(listDistributorNameMaster.DistributorID)) throw new System.ArgumentException("Insufficient save", "Save undo");
                masterDataTable = this.MasterTableAdapter.GetData(listDistributorNameMaster.DistributorID);
                if (masterDataTable.Count > 0) masterRow = masterDataTable[0]; else throw new System.ArgumentException("Insufficient save", "Get for edit");
            }

            masterRow.EntryDate = this.listDistributorNameMaster.EntryDate;
            
            masterRow.DistributorCategoryID = this.listDistributorNameMaster.DistributorCategoryID;
            masterRow.DistributorTypeID = this.listDistributorNameMaster.DistributorTypeID;
            masterRow.AddressAreaID = this.listDistributorNameMaster.AddressAreaID;

            masterRow.Description = this.listDistributorNameMaster.Description;
            masterRow.DescriptionOfficial = this.listDistributorNameMaster.DescriptionOfficial;
            masterRow.DescriptionOriginal = this.listDistributorNameMaster.DescriptionOriginal;
            masterRow.VATCode = this.listDistributorNameMaster.VATCode;
            masterRow.ContactName = this.listDistributorNameMaster.ContactName;
            masterRow.AttentionName = this.listDistributorNameMaster.AttentionName;
            masterRow.Telephone = this.listDistributorNameMaster.Telephone;
            masterRow.EMailAddress = this.listDistributorNameMaster.EMailAddress;
            masterRow.AddressNo = this.listDistributorNameMaster.AddressNo;
            masterRow.AddressStreet = this.listDistributorNameMaster.AddressStreet;
            masterRow.Other = this.listDistributorNameMaster.Other;

            masterRow.InActive = this.listDistributorNameMaster.InActive;

            masterRow.UserID = this.UserOrganization.UserID;
            masterRow.UserOrganizationID = this.UserOrganization.UserOrganizationID;

            if (this.listDistributorNameMaster.DistributorID <= 0) masterDataTable.AddListDistributorNameRow(masterRow);

            int rowsAffected = this.MasterTableAdapter.Update(masterRow);

            distributorID = masterRow.DistributorID;

            return rowsAffected == 1;

        }


        private bool SaveDetail(int distributorID)
        {
            int serialID = 0; int rowsAffected = 0;


            #region <Save collection>

            serialID = 0;

            ListDistributorNameDTS.ListDistributorNameDetailDataTable detailDataTable = new ListDistributorNameDTS.ListDistributorNameDetailDataTable();

            foreach (ListDistributorNameDetail listDistributorNameDetail in this.ListDistributorNameDetailList)
            {
                ListDistributorNameDTS.ListDistributorNameDetailRow detailRow = detailDataTable.NewListDistributorNameDetailRow();

                detailRow.DistributorID = distributorID;
                detailRow.SerialID = ++serialID;

                detailRow.DistributorModelID = listDistributorNameDetail.DistributorModelID;
                detailRow.Reference = listDistributorNameDetail.Reference;
                detailRow.Description = listDistributorNameDetail.Description;
                detailRow.Other = listDistributorNameDetail.Other;

                detailRow.InActive = false;

                detailDataTable.AddListDistributorNameDetailRow(detailRow);
            }

            rowsAffected = this.DetailTableAdapter.Update(detailDataTable);
            if (rowsAffected != this.listDistributorNameDetailList.Count) throw new System.ArgumentException("Insufficient save", "Save detail");


            #endregion <Save collection>


            return true;
        }


        private bool SaveUndo(int distributorID)
        {
            this.DetailTableAdapter.Delete(distributorID);

            return true;
        }



        #endregion



        private void RestoreProcedure()
        {
            string queryString;
            string[] queryArray;



            queryString = "     " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";

            queryString = queryString + "       SELECT      CAST(0 AS bit) AS IsDistributor, 100000 * DistributorCategoryID AS DistributorID, Description, 100000 * AncestorID AS AncestorID, EntryDate, '' AS CategoryName, '' AS AreaName , '' AS TypeName  " + "\r\n";
            queryString = queryString + "       FROM        ListDistributorCategory " + "\r\n";

            queryString = queryString + "       UNION ALL " + "\r\n";

            queryString = queryString + "       SELECT      CAST(1 AS bit) AS IsDistributor, ListDistributorName.DistributorID AS DistributorID, ListDistributorName.DescriptionOfficial + ' ' + ListDistributorName.DescriptionOriginal AS Description, 100000 * ListDistributorName.DistributorCategoryID AS AncestorID, ListDistributorName.EntryDate, ListDistributorCategory.Description AS CategoryName, ListAddressArea.Description AS AreaName , ListDistributorType.Description AS TypeName " + "\r\n";
            queryString = queryString + "       FROM        ListDistributorName INNER JOIN " + "\r\n";
            queryString = queryString + "                   ListDistributorCategory ON ListDistributorName.DistributorCategoryID = ListDistributorCategory.DistributorCategoryID  INNER JOIN " + "\r\n";
            queryString = queryString + "                   ListAddressArea ON ListDistributorName.AddressAreaID = ListAddressArea.AddressAreaID INNER JOIN " + "\r\n";
            queryString = queryString + "                   ListDistributorType ON ListDistributorName.DistributorTypeID = ListDistributorType.DistributorTypeID  " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameListing", queryString);






            queryString = "     @DistributorID Int " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       SELECT      DistributorID, DistributorCategoryID, DistributorTypeID, Description, DescriptionOfficial, DescriptionOriginal, VATCode, ContactName, AttentionName, Telephone, EMailAddress, AddressNo, AddressStreet, AddressAreaID, InActive, Other, UserID, UserOrganizationID, EntryDate " + "\r\n";
            queryString = queryString + "       FROM        ListDistributorName " + "\r\n";
            queryString = queryString + "       WHERE       DistributorID = @DistributorID " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameSelect", queryString);


            queryString = "     @DistributorCategoryID int, @DistributorTypeID int, @Description nvarchar(100), @DescriptionOfficial nvarchar(100), @DescriptionOriginal nvarchar(100), @VATCode nvarchar(60), @ContactName nvarchar(100), @AttentionName nvarchar(60), @Telephone nvarchar(60), @EMailAddress nvarchar(60), @AddressNo nvarchar(60), @AddressStreet nvarchar(150), @AddressAreaID int, @InActive bit, @Other nvarchar(100), @UserID int, @UserOrganizationID int, @EntryDate datetime " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       INSERT INTO ListDistributorName (DistributorCategoryID, DistributorTypeID, Description, DescriptionOfficial, DescriptionOriginal, VATCode, ContactName, AttentionName, Telephone, EMailAddress, AddressNo, AddressStreet, AddressAreaID, InActive, Other, UserID, UserOrganizationID, EntryDate) VALUES (@DistributorCategoryID, @DistributorTypeID, @Description, @DescriptionOfficial, @DescriptionOriginal, @VATCode, @ContactName, @AttentionName, @Telephone, @EMailAddress, @AddressNo, @AddressStreet, @AddressAreaID, @InActive, @Other, @UserID, @UserOrganizationID, @EntryDate) " + "\r\n";
            queryString = queryString + "       SELECT      DistributorID, DistributorCategoryID, DistributorTypeID, Description, DescriptionOfficial, DescriptionOriginal, VATCode, ContactName, AttentionName, Telephone, EMailAddress, AddressNo, AddressStreet, AddressAreaID, InActive, Other, UserID, UserOrganizationID, EntryDate FROM ListDistributorName WHERE DistributorID = SCOPE_IDENTITY() " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameInsert", queryString);


            queryString = "     @DistributorID int, @DistributorCategoryID int, @DistributorTypeID int, @Description nvarchar(100), @DescriptionOfficial nvarchar(100), @DescriptionOriginal nvarchar(100), @VATCode nvarchar(60), @ContactName nvarchar(100), @AttentionName nvarchar(60), @Telephone nvarchar(60), @EMailAddress nvarchar(60), @AddressNo nvarchar(60), @AddressStreet nvarchar(150), @AddressAreaID int, @InActive bit, @Other nvarchar(100), @UserID int, @UserOrganizationID int, @EntryDate datetime " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       UPDATE      ListDistributorName SET DistributorCategoryID = @DistributorCategoryID, DistributorTypeID = @DistributorTypeID, Description = @Description, DescriptionOfficial = @DescriptionOfficial, DescriptionOriginal = @DescriptionOriginal, VATCode = @VATCode, ContactName = @ContactName, AttentionName = @AttentionName, Telephone = @Telephone, EMailAddress = @EMailAddress, AddressNo = @AddressNo, AddressStreet = @AddressStreet, AddressAreaID = @AddressAreaID, InActive = @InActive, Other = @Other, UserID = @UserID, UserOrganizationID = @UserOrganizationID, EntryDate = @EntryDate WHERE DistributorID = @DistributorID " + "\r\n";
            queryString = queryString + "       SELECT      DistributorID, DistributorCategoryID, DistributorTypeID, Description, DescriptionOfficial, DescriptionOriginal, VATCode, ContactName, AttentionName, Telephone, EMailAddress, AddressNo, AddressStreet, AddressAreaID, InActive, Other, UserID, UserOrganizationID, EntryDate FROM ListDistributorName WHERE DistributorID = @DistributorID " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameUpdate", queryString);


            queryString = " @DistributorID int ";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       DELETE FROM ListDistributorName WHERE DistributorID = @DistributorID " + "\r\n";
            SQLDatabase.CreateStoredProcedure("ListDistributorNameDelete", queryString);






            queryString = " @DistributorID int " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       SELECT      DistributorID, SerialID, DistributorModelID, Reference, Description, Other, InActive" + "\r\n";
            queryString = queryString + "       FROM        ListDistributorNameDetail " + "\r\n";
            queryString = queryString + "       WHERE       DistributorID = @DistributorID " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameDetailSelect", queryString);


            queryString = " @DistributorID int, @SerialID int, @DistributorModelID int, @Reference nvarchar(100), @Description nvarchar(100), @Other nvarchar(100), @InActive bit " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       INSERT INTO ListDistributorNameDetail (DistributorID, SerialID, DistributorModelID, Reference, Description, Other, InActive) VALUES (@DistributorID, @SerialID, @DistributorModelID, @Reference, @Description, @Other, @InActive) " + "\r\n";
            queryString = queryString + "       SELECT      DistributorID, SerialID, DistributorModelID, Reference, Description, Other, InActive FROM ListDistributorNameDetail WHERE (DistributorID = @DistributorID) AND (SerialID = @SerialID) " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameDetailInsert", queryString);



            queryString = " @DistributorID int, @SerialID int, @DistributorModelID int, @Reference nvarchar(100), @Description nvarchar(100), @Other nvarchar(100), @InActive bit " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       UPDATE      ListDistributorNameDetail SET DistributorID = @DistributorID, SerialID = @SerialID, DistributorModelID = @DistributorModelID, Reference = @Reference, Description = @Description, Other = @Other, InActive = @InActive WHERE DistributorID = @DistributorID AND SerialID = @SerialID " + "\r\n";
            queryString = queryString + "       SELECT      DistributorID, SerialID, DistributorModelID, Reference, Description, Other, InActive FROM ListDistributorNameDetail WHERE DistributorID = @DistributorID AND SerialID = @SerialID " + "\r\n";

            SQLDatabase.CreateStoredProcedure("ListDistributorNameDetailUpdate", queryString);



            queryString = " @DistributorID int " + "\r\n";
            queryString = queryString + " WITH ENCRYPTION " + "\r\n";
            queryString = queryString + " AS " + "\r\n";
            queryString = queryString + "       DELETE FROM ListDistributorNameDetail WHERE DistributorID = @DistributorID " + "\r\n";
            SQLDatabase.CreateStoredProcedure("ListDistributorNameDetailDelete", queryString);


      
            /// <summary>
            /// Check for editable
            /// </summary>
            queryArray = new string[10];                      
            queryArray[0] = "   SELECT      TOP 1 DistributorID FROM ListAddressAreaColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[1] = "   SELECT      TOP 1 DistributorID FROM ListCustomerGroupColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[2] = "   SELECT      TOP 1 DistributorID FROM ListCustomerName WHERE DistributorID = @FindIdentityID ";
            queryArray[3] = "   SELECT      TOP 1 DistributorID FROM ListCustomerNameColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[4] = "   SELECT      TOP 1 DistributorID FROM ListCustomerTypeColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[5] = "   SELECT      TOP 1 DistributorID FROM ListCustomerChannelColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[6] = "   SELECT      TOP 1 DistributorID FROM ListDistributorColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[7] = "   SELECT      TOP 1 DistributorID FROM ListDistributorFile WHERE DistributorID = @FindIdentityID ";
            queryArray[8] = "   SELECT      TOP 1 DistributorID FROM ListItemCommodityColumn WHERE DistributorID = @FindIdentityID ";
            queryArray[9] = "   SELECT      TOP 1 DistributorID FROM InMarketMaster WHERE DistributorID = @FindIdentityID ";

            SQLDatabase.CreateProcedureToCheckExisting("ListDistributorNameEditable", queryArray);
        }
    }
}
